﻿// 5. Write a method that checks if the element at given position in given array of integers is bigger than its two neighbors 
// (when such exist).

using System;

class IfElementIsBigger
{
    static void Main()
    {
        Console.Write("Enter the position for check : ");
        int position = int.Parse(Console.ReadLine());

        Console.Write("Enter the length of the array : ");
        int length = int.Parse(Console.ReadLine());

        if (length < 0)
        {
            Console.WriteLine("Array with negative length cannot exist.");
            return;
        }
        else if (position >= length)
        {
            Console.WriteLine("The chosen index is out of the range of the array.");
            return;
        }
        else
        {
            Console.WriteLine("Enter values for the array : ");
            int[] array = new int[length];
            for (int i = 0; i < length; i++)
            {
                Console.Write("array[{0}] = ", i);
                array[i] = int.Parse(Console.ReadLine());
            }

            if (array.Length == 0)
            {
                Console.WriteLine("The array has no elements.");
            }
            else if (array.Length == 1)
            {
                Console.WriteLine("The array's length is 1 and the its element doesn't have neighbours.");
            }
            else
            {
                bool result = CheckElementNeighbours(position, array);
                if (result)
                {
                    Console.WriteLine("The element at the given position is bigger than its neighbours.");
                }
                else
                {
                    Console.WriteLine("The element at the given position is NOT bigger than its neighbours.");
                }
            }
        }
    }

    static bool CheckElementNeighbours(int position, int[] array)
    {
        bool result = false;
        if (position == 0)
        {
            if (array[position] > array[position + 1])
            {
                result = true;
            }
        }
        else if (position == array.Length - 1)
        {
            if (array[position] > array[position - 1])
            {
                result = true;
            }
        }
        else
        {
            if (array[position] > array[position - 1] && array[position] > array[position + 1])
            {
                result = true;
            }
        }

        return result;
    }
}
